package com.learning.jquizenglish.screen.doquiz;

import java.util.ArrayList;

import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.TextView;

import com.learning.jquizenglish.R;
import com.learning.jquizenglish.activity.DoQuizActivity;
import com.learning.jquizenglish.common.fragment.JQuizEnglishFragment;
import com.learning.jquizenglish.common.fragment.JQuizTaskFragment;
import com.learning.jquizenglish.model.JQuizRequestModel;
import com.learning.jquizenglish.model.ProfileModel;
import com.learning.jquizenglish.model.QuestionModel;
import com.learning.jquizenglish.model.ResultModel;
import com.learning.jquizenglish.model.SaveScoreData;
import com.learning.jquizenglish.screen.profile.ProfilePreference;

public class ResultQuizView {

	private JQuizEnglishFragment mFrag;
	private ExpandableListView mListResults;
	private ArrayList<QuestionModel> mListQuestions;
	private View root;
	private FragmentActivity fragActivity;
	
	public ResultQuizView(JQuizEnglishFragment fragScreen) {
		mFrag = fragScreen;
	}
	
	public void setData(ArrayList<QuestionModel> listQuestion){
		mListQuestions = listQuestion;
	}
	
	public void saveScore(final FragmentActivity fragActivity,final View contentView){
		JQuizRequestModel requestModel = DoQuizSession.getInstance().getRequestSaveScore(fragActivity);
		mFrag.executePostTask(requestModel, false, ResultModel.class, new JQuizTaskFragment.JQuizTaskListener() {
			
			@Override
			public void onTaskComplete(Object object) {
				if (object!=null) {
					((DoQuizActivity)fragActivity).setResultOk();
				}
				init(fragActivity, contentView);
			}
		});
	}
	
	public void init(FragmentActivity fragActivity,View contentView){
		this.root = contentView;
		this.fragActivity = fragActivity;
		showData();
	}
	
	private void showData(){
		mListResults = (ExpandableListView) root.findViewById(R.id.lvResult);
		root.findViewById(R.id.progressbar).setVisibility(View.GONE);
		if(mListQuestions!=null){
			String title = "Correct "+getNumberAnswerCorrect()+"/"+(mListQuestions.size());
			((TextView)root.findViewById(R.id.tvTitle)).setText(title);
		}
//		ViewTreeObserver localViewTreeObserver = mListResults.getViewTreeObserver();
		mListResults.setGroupIndicator(null);
//		mListResults.setIndicatorBounds(-45 + mListResults.getRight(), mListResults.getWidth());
//	      localViewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener()
//	      {
//	        public void onGlobalLayout()
//	        {
////	          if (Build.VERSION.SDK_INT < 18)
////	          {
////	        	  mListResults.setIndicatorBounds(-36 + mListResults.getRight(), mListResults.getWidth());
////	            return;
////	          }
////	          	mListResults.setIndicatorBoundsRelative(-36 + mListResults.getRight(), mListResults.getWidth());
//	        	 mListResults.setIndicatorBounds(-(mListResults.getContext().getResources().getDimensionPixelSize(R.dimen.icon_header_size)) 
//	        			 + mListResults.getRight(), mListResults.getWidth());
//	        }
//	      });
		ResultAdapter adapter = new ResultAdapter(fragActivity,mListQuestions);
		mListResults.setAdapter(adapter);
		if(mListQuestions==null) return;
		int size = mListQuestions.size();
		for (int i = 0; i < size; i++) {
			mListResults.expandGroup(i);
		}
	}
	
	public int getNumberAnswerCorrect(){
		int ret = 0;
		int size = mListQuestions.size();
		for (int i = 0; i < size; i++) {
			QuestionModel model = mListQuestions.get(i);
			if (model!=null && model.isCorrectAnswer()) {
				ret++;
			}
		}
		return ret;
	}
}
